package com.qf.quduba.controller;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.qf.quduba.VO.ResultVO;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 17312
 * @time2020/10/1610:34
 */
@RestController
@CrossOrigin
public class NoteController {

    @GetMapping("/getCode")
    public ResultVO getCode(@RequestParam String phone){
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G6Y8GvM636yFzzVMKkN", "sRqREMNiGTyBHzAbyuVuYz58epuXQk");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "趣读吧");
        request.putQueryParameter("TemplateCode", "SMS_204745893");
        String s = (int) (Math.random() * 9000 + 1000)+"";
        request.putQueryParameter("TemplateParam", "{code:"+s+"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            String jsonStr = response.getData();
            JSONObject jsonObj = JSONObject.parseObject(jsonStr);
            String message = jsonObj.getString("Message");
            if("OK".equals(message)){
                return new ResultVO(1000,"success",s);
            }else {
                return new ResultVO(1001,"fail",null);
            }

        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return null;
    }


}
